package gases.as3.librarys.components.ui.control
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	import gases.as3.librarys.components.events.GButtonEvent;
	import gases.as3.librarys.components.skin.SkinStylesMangaer;
	import gases.as3.librarys.components.skin.data.BaseSkin;
	import gases.as3.librarys.components.skin.data.ScrollSkin;
	import gases.as3.librarys.components.ui.GSkinBox;
	import gases.as3.librarys.components.ui.GUIComponents;
	import gases.as3.util.SkinStyleName;
	
	public class GScrollBar extends GUIComponents
	{
		/**
		 *滚动条组件，暂时作用对象仅支持Sprite
		 * <p>
		 * <p>更新滚动条通过方法 updateScroll
		 * @param w 滚动条宽度，实际参考美术给的UI
		 * @param h 滚动条高度
		 * @param tagetSprite 目标对象
		 * @param isVertical 布尔值，true为垂直。可以不填 ，默认为true,暂时没有写横着的滚动条
		 * @param upSkin 向上按钮的skin
		 * @param upSkin 向下按钮的skin
		 * @param scrollSkin 滑块按钮的skin
		 * @see GUIComepments
		 * @author  gases
		 */		
		public function GScrollBar(width:Number,height:Number,tagetSprite:DisplayObject = null,isVertical:Boolean = true,
								   upSkin:BaseSkin = null,downSkin:BaseSkin = null,scrollSkin:BaseSkin = null)
		{
			_width = 			width;
			_height = 			height;
			_target = 			tagetSprite;
			_isVertical = 		isVertical;
			_btnDownSkin = 		downSkin;
			_btnUpSkin = 		upSkin;
			_btnScrollSkin = 	scrollSkin;
			super();
		}
		
		override protected function initialization():void
		{
			super.initialization();
			this.mouseEnabled = true;
			if(!_btnUpSkin)
			{
				_btnUpSkin = SkinStylesMangaer.instance.getSkinData(SkinStyleName.DEFAULT_SCROLL_UP_BTN) as BaseSkin;
			}
			if(!_btnDownSkin)
			{
				_btnDownSkin = SkinStylesMangaer.instance.getSkinData(SkinStyleName.DEFAULT_SCROLL_DOWN_BTN) as BaseSkin;
			}
			if(!_btnScrollSkin)
			{
				_btnScrollSkin = SkinStylesMangaer.instance.getSkinData(SkinStyleName.DEFAULT_SCROLL_BTN) as BaseSkin;
			}
			
			_scrollMoveTimer = new Timer(100);
			_scrollMoveTimer.addEventListener(TimerEvent.TIMER, scrollMoveTimerEvent);
			
			_btnMouseDownTimer = new Timer(500);
			_btnMouseDownTimer.addEventListener(TimerEvent.TIMER, btnMouseDownTimerEvent);
			
			_btnMoveTimer = new Timer(100);
			_btnMoveTimer.addEventListener(TimerEvent.TIMER, btnMoveTimerEvent);

			initUI();
			
		}
		
		override public function set skin(value:BaseSkin):void
		{
			super.skin = value;
			_scrollSkin = value as ScrollSkin;
			_scrollBarBg.source = _scrollSkin.bg_scroll;
			
			initUIPostion();
			addEventListeners();
			updateScroll();
			px = 20;
		}
		
		override public function dispose():void
		{
			removeEventListenrs();
			super.dispose();
		}
		
		public function setLastIndex() : void {
			_btnScrollBar.y = Math.floor(_scrollBarBg.y + _scrollBarBg.height - _btnScrollBar.height);
			moveMaskee();
		}
		
		public function setScrollBarPos(p : Number) : void {
			_btnScrollBar.y = p;
			moveMaskee();
		}
		
		
		/**
		 *设置滚动像素 
		 * @param i
		 * 
		 */		
		public function set px(i:int):void
		{
			var percent:Number = (i/(_target.height - _height))*100;
			_px = (_scrollBarBg.height - _btnScrollBar.height) * percent / 100;
		}
		
		/**
		 *更新滚动条 
		 * 
		 */		
		public function updateScroll():void
		{
			if(_target.height < _height)
			{
				removeEventListenrs();
				_target.y = _initTagetY;
				_btnScrollBar.y = _initRectangleY;
				this.visible = false;
			}else
			{
				updateUI();
				moveMaskee();
				addEventListeners();
				this.visible = true;
			}
		}
		
		private function addEventListeners():void
		{
			_btnScrollBar.addEventListener(GButtonEvent.BUTTON_DOWN, 	scrollMouseDown);
			_btnScrollBar.addEventListener(GButtonEvent.BUTTON_UP, 		buttonscrollMouseUp);
			_scrollBarBg.addEventListener(MouseEvent.MOUSE_DOWN, 		scrollBgMouseDown);
			this.addEventListener(MouseEvent.MOUSE_WHEEL, 				btnMouseWheel);
			this.addEventListener(MouseEvent.MOUSE_UP, 					scrollMouseUp);
            _target.addEventListener(MouseEvent.MOUSE_WHEEL, 			btnMouseWheel);
			if(_btnUp != null){
				_btnUp.addEventListener(GButtonEvent.BUTTON_DOWN, 		btnUpMouseDown);
				_btnUp.addEventListener(GButtonEvent.BUTTON_OUT, 		btnMouseOutAndUp);
				_btnUp.addEventListener(GButtonEvent.BUTTON_UP, 		btnMouseOutAndUp);
			}
			if(_btnDown != null){
				_btnDown.addEventListener(GButtonEvent.BUTTON_DOWN, 	btnDownMouseDown);
				_btnDown.addEventListener(GButtonEvent.BUTTON_OUT, 		btnMouseOutAndUp);
				_btnDown.addEventListener(GButtonEvent.BUTTON_UP, 		btnMouseOutAndUp);
			}
		}
		
		private function removeEventListenrs():void
		{
			_btnScrollBar.removeEventListener(GButtonEvent.BUTTON_DOWN, 	scrollMouseDown);
			_btnScrollBar.removeEventListener(GButtonEvent.BUTTON_UP, 		buttonscrollMouseUp);
			_scrollBarBg.removeEventListener(MouseEvent.MOUSE_DOWN, 		scrollBgMouseDown);
			this.removeEventListener(MouseEvent.MOUSE_WHEEL, 				btnMouseWheel);
			this.removeEventListener(MouseEvent.MOUSE_UP, 					scrollMouseUp);
			_target.removeEventListener(MouseEvent.MOUSE_WHEEL, 			btnMouseWheel);
			if(_btnUp != null){
				_btnUp.removeEventListener(GButtonEvent.BUTTON_DOWN, 		btnUpMouseDown);
				_btnUp.removeEventListener(GButtonEvent.BUTTON_OUT, 		btnMouseOutAndUp);
				_btnUp.removeEventListener(GButtonEvent.BUTTON_UP, 		btnMouseOutAndUp);
			}
			if(_btnDown != null){
				_btnDown.removeEventListener(GButtonEvent.BUTTON_DOWN, 	btnDownMouseDown);
				_btnDown.removeEventListener(GButtonEvent.BUTTON_OUT, 		btnMouseOutAndUp);
				_btnDown.removeEventListener(GButtonEvent.BUTTON_UP, 		btnMouseOutAndUp);
			}
		}
		
		/**
		 *滑块鼠标down 
		 * @param event
		 * 
		 */		
		private function scrollMouseDown(event:GButtonEvent):void
		{
			_btnScrollBar.startDrag(false, _rectangle);
			_scrollMoveTimer.start();
			_btnScrollBar.stage.addEventListener(MouseEvent.MOUSE_UP, 		scrollMouseUp);
			_btnScrollBar.addEventListener(MouseEvent.MOUSE_UP, 		scrollMouseUp);
		}
		
		/**
		 *滑块鼠标up 
		 * @param event
		 * 
		 */		
		private function scrollMouseUp(event:MouseEvent):void
		{
			_btnScrollBar.stage.removeEventListener(MouseEvent.MOUSE_UP, 		scrollMouseUp);
			_btnScrollBar.y = Math.floor(_btnScrollBar.y);
			_btnScrollBar.stopDrag();
			_scrollMoveTimer.stop();
		}
		
		/**
		 *滑块鼠标up 
		 * @param event
		 * 
		 */		
		private function buttonscrollMouseUp(event:GButtonEvent):void
		{
			_btnScrollBar.stage.removeEventListener(MouseEvent.MOUSE_UP, 		scrollMouseUp);
			_btnScrollBar.y = Math.floor(_btnScrollBar.y);
			_btnScrollBar.stopDrag();
			_scrollMoveTimer.stop();
		}
		
		/**
		 *点击滑块槽 
		 * @param event
		 * 
		 */		
		private function scrollBgMouseDown(event:MouseEvent):void
		{
			moveScroll(_btnScrollBar.parent.mouseY);
			event.updateAfterEvent();
		}
		
		/**
		 *鼠标按下up 
		 * @param event
		 * 
		 */		
		private function btnUpMouseDown(event:GButtonEvent):void
		{
			moveScroll(_btnScrollBar.y - _px);
			_curBtnType = "up";
			_btnMouseDownTimer.start();
		}
		
		/**
		 *鼠标按下down 
		 * @param event
		 * 
		 */		
		private function btnDownMouseDown(event:GButtonEvent):void
		{
			moveScroll(_btnScrollBar.y + _btnScrollBar.height + _px);
			_curBtnType = "down";
			_btnMouseDownTimer.start();
		}
		
		/**
		 *鼠标移进去移出去 
		 * @param event
		 * 
		 */		
		private function btnMouseOutAndUp(event:GButtonEvent):void
		{
			_btnMouseDownTimer.stop();
			_btnMoveTimer.stop();
		}
		
		/**
		 *鼠标滚动 
		 * @param event
		 * 
		 */		
		private function btnMouseWheel(event:MouseEvent):void
		{
			if(event.delta < 0){
				moveScroll(_btnScrollBar.y + _btnScrollBar.height + _px);
			}else{
				moveScroll(_btnScrollBar.y - _px);
			}
		}
		
		/**
		 *滑块移动Timer 
		 * @param event
		 * 
		 */		
		private function scrollMoveTimerEvent(event:TimerEvent):void
		{
			moveMaskee();
			event.updateAfterEvent();
		}
		
		/**
		 *一直按下鼠标,up,down 
		 * @param event
		 * 
		 */		
		private function btnMouseDownTimerEvent(event:TimerEvent):void
		{
			_btnMouseDownTimer.stop();
			_btnMoveTimer.start();
			event.updateAfterEvent();
		}
		
		/**
		 *滚动timer 
		 * @param event
		 * 
		 */		
		private function btnMoveTimerEvent(event:TimerEvent):void
		{
			if(_curBtnType == "up"){
				moveScroll(_btnScrollBar.y - _px);
			}else{
				moveScroll(_btnScrollBar.y + _btnScrollBar.height + _px);
			}
			event.updateAfterEvent();
		}
		
		/**
		 *移动滑块 
		 * @param iY
		 * 
		 */		
		private function moveScroll(iY:int):void
		{
			if (iY > _btnScrollBar.y + _btnScrollBar.height) {
				iY = Math.floor(iY - _btnScrollBar.height);
				_btnScrollBar.y = ((iY + _btnScrollBar.height + 6) > (_scrollBarBg.y + _scrollBarBg.height)) ? Math.floor(_scrollBarBg.y + _scrollBarBg.height - _btnScrollBar.height) : iY;
			} else {
				_btnScrollBar.y = (iY < (_scrollBarBg.y + 6)) ? _scrollBarBg.y : iY;
			}
			if (_btnScrollBar.y > Math.floor(_scrollBarBg.y + _scrollBarBg.height - _btnScrollBar.height)) {
				_btnScrollBar.y = Math.floor(_scrollBarBg.y + _scrollBarBg.height - _btnScrollBar.height)
			}
			moveMaskee();
		}
		
		/**
		 *更新目标sprite的y 
		 * 
		 */		
		private function moveMaskee():void
		{
			var percent:Number = (_target.height - _height)/100;
			var remove:Number = (_btnScrollBar.y - _initRectangleY)/(_scrollBarBg.height-_btnScrollBar.height) * 100;
			_target.y = -(Math.floor(remove * percent - _initTagetY));
		}
		
		private function initUI():void
		{
			_btnUp = 	new GButton();
			_btnUp.skin = _btnUpSkin;
			 
			_btnDown =   new GButton();
			_btnDown.skin = _btnDownSkin;
			
			_btnScrollBar = new GButton();
			_btnScrollBar.skin = _btnScrollSkin;
			
			_scrollBarBg = new GSkinBox();
			
			_maskSprite = new Sprite();
			if(!_target)
			{
				 _target = new Sprite();
				(_target as Sprite).graphics.beginFill(0xFFFFFF);
				(_target as Sprite).graphics.drawRect(0, 0, 180, 200);
				(_target as Sprite).graphics.endFill();
			}
			else
			{
				_maskSprite.graphics.beginFill(0xFFFFFF);
				_maskSprite.graphics.drawRect(_target.x, _target.y, _width, _height);
				_maskSprite.graphics.endFill();
				_target.parent.addChild(_maskSprite);
				_target.mask = _maskSprite;
			}
			
			if(!_scrollBarBg.source)
			{
				_scrollBarBg.source = (SkinStylesMangaer.instance.getSkinData(SkinStyleName.DEFAULT_SCROLL) as ScrollSkin).bg_scroll;
			}
			this.addChild(_btnUp);
			this.addChild(_btnDown);
			this.addChild(_btnScrollBar);
			this.addChildAt(_scrollBarBg,0);
			
			initUIPostion();
			addEventListeners();
			updateScroll();
			px = 20;
		}
		
		private function initUIPostion():void
		{
			_scrollBarBg.y = 	  _btnUp.height;
			_scrollBarBg.setHeight( _height - _btnUp.height - _btnDown.height);
			_scrollBarBg.mouseEnabled = true;
			_btnScrollBar.y = _btnUp.height;
			_btnScrollBar.height = _height * _scrollBarBg.height/ _target.height;
			_btnDown.y = _btnUp.height + _scrollBarBg.height;
			_rectangle = new Rectangle(Math.floor(_scrollBarBg.x), Math.floor(_scrollBarBg.y));
			_rectangle.width =  0 ;
			_rectangle.height =  _scrollBarBg.height - _btnScrollBar.height;
			_initTagetY = _target.y;
			_initRectangleY =  _rectangle.y;
			
		}
		
		private function updateUI():void
		{
			_btnScrollBar.height = _height * _scrollBarBg.height/ _target.height;
			_btnScrollBar.y = (_initTagetY - _target.y)*(_scrollBarBg.height-_btnScrollBar.height)/(_target.height - _height) +  _initRectangleY;
			if(_btnScrollBar.y > _scrollBarBg.height - _btnScrollBar.height + _scrollBarBg.y)
			{
				_btnScrollBar.y = _scrollBarBg.height - _btnScrollBar.height + _scrollBarBg.y;
			}
			_rectangle = new Rectangle(Math.floor(_scrollBarBg.x), Math.floor(_scrollBarBg.y));
			_rectangle.width =  0 ;
			_rectangle.height =  _scrollBarBg.height - _btnScrollBar.height;
		}
		
		private var _btnUp:GButton;//向下按钮
		private var _btnDown:GButton;//向上按钮
		private var _btnScrollBar:GButton;//滑块
		private var _scrollBarBg:GSkinBox;//背景
		private var _scrollSkin:ScrollSkin;
		
		private var _maskSprite:Sprite;//遮罩
		private var _rectangle:Rectangle;
		
		private var _btnUpSkin:BaseSkin;
		private var _btnDownSkin:BaseSkin;
		private var _btnScrollSkin:BaseSkin;
		
		private var _scrollMoveTimer:Timer;	//用于移动taget
		private var _btnMouseDownTimer:Timer;//鼠标在上或下按钮上按下而未释放未移开，不断进行滚动
		private var _btnMoveTimer:Timer;//鼠标在上或下按钮上按下而未释放未移开
		
		private var _px:Number;//滑轮滚动一次以及按钮点击一次，被滚动对象移动的像素，默认为10像素
		private var _curBtnType:String;
		private var _initTagetY:int;//初始化 目标的y
		private var _initRectangleY:int;//初始化滑块的y
		
		private var _width:Number;
		private var _height:Number;
		private var _target:DisplayObject;
		private var _isVertical:Boolean = true;
		
		private var _initFirstTargetY:Number;
		
	}
}